# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import datetime
import json
import os
#import pandas as pd
# from turtle import pd
import openpyxl

global excelpath
global codePath
global bytePath

def start():
    global excelpath
    global codePath
    global bytePath
    global root_path
    now_time = datetime.datetime.now()
    configtxt = open("config.txt")
    jsonData = json.load(configtxt)
    excelpath = jsonData['excelPath']
    codePath = jsonData['codePath']
    bytePath = jsonData['bytePath']
    project_path = os.path.abspath(os.path.dirname(__file__))
    # Mac、Linux、Unix
    if project_path.find('/') != -1: separator = '/'

    p_name = 'pythonProject'
    root_path = project_path[:project_path.find(f'{p_name}{separator}') + len(f'{p_name}{separator}')]

    parse_excel()
    configtxt.close()



def parse_excel():
    global excelpath
    global codePath
    global bytePath

    excelNamelist = []
    for root, dirs, files in os.walk(excelpath):
        for f in files:
            if f.endswith(".xlsx") and not f.startswith('._'):
                excelNamelist.append(f)
    for fileName in excelNamelist:
        excelUrl = excelpath + "/"+fileName
        codeUrl = codePath + "/"+fileName
        byteUrl = bytePath+ "/"+fileName
        GenCodeAndBytes(excelUrl,codeUrl,byteUrl,fileName)

def GenCodeAndBytes(excelUrl,codeUrl,byteUrl,fileName):
    global root_path
    global datas
    workbook = openpyxl.load_workbook(excelUrl)
    sheet = workbook.active
    datas = []
    for row in sheet.iter_rows(values_only=True):
        datas.append([cell for cell in row])


    # 构建c#代码
    GenCode(fileName[:-5] ,datas)
    # col = 3
    # while col<maxcol:



    # workbook = xlrd.open_workbook(excelUrl)
    # table = workbook.sheet_by_index(0)
    # typesColArray = []
    # nameColArray = []
    # for row_num in range(table.nrows):
    #     if row_num >1:
    #         for col_num in range(table.ncols):
    #             #     valueName = table.cell_value(1,col_num)
    #             #     valueType =  table.cell_value(2,col_num)
    #             if col_num > 0:
    #                 if row_num == 1:
    #                     nameColArray[col_num-1] = table.cell_value(row_num,col_num)
    #                 if row_num == 2:
    #                     typesColArray[col_num-1] = table.cell_value(row_num,col_num)
    #                 else:
    #                     print(table.cell_value(row_num,col_num))


def GenCode(resName,typeArray):
    keyNameData = typeArray[1]
    typeNameData = typeArray[2]
    codestr = f"public class RES_{resName}\n"
    codestr += "{\n"
    for index, key in enumerate(keyNameData):
        if index>0:
            codestr += GenTypeStr(key,typeNameData[index])
    codestr += "}\n"
    f = open("TestResource.cs","w")
    f.write(codestr)
    f.close()
def GenTypeStr(name,typeName):
    empty = "    "
    nextEmpty = "        "
    subStr = f"{empty}private {typeName} m_{name};\n{empty}public {typeName} {name}\n{empty}{{\n{nextEmpty}get{{ return m_{typeName}; }}\n{nextEmpty}set{{ m_{typeName} = value; }}\n{empty}}}\n"
    return subStr

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    start()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
